package com.dins.elashutina.filter;

import com.dins.elashutina.parser.Node;
import com.dins.elashutina.parser.Tree;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: ekaterina.lashutina
 * Date: 16.09.13
 * Time: 12:14
 */
public class Filter {
    public static void usedMethods(Tree tree) {
        InputStream inp;
        try {
            inp = new FileInputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\filtered.xls");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            wb.setSheetName(0, "coberturaReport used methods");
            for (int index = sheet.getLastRowNum(); index >= sheet.getFirstRowNum(); index--) {
                if (sheet.getRow(index) == null) {
                    continue;
                }
                sheet.removeRow(sheet.getRow(index));
            }
            int i = 0;
            for (Node node : tree) {
                double sum = 0.0;
                if (tree.getType() == Tree.Type.COBERTURA) {
                    for (String str : (node.getValues())) {
                        sum += Double.parseDouble(str);
                    }
                    if (sum == 0.0) {
                        continue;
                    } else {
                        if (i == 0) {
                            Row row0 = sheet.createRow(0);
                            row0.createCell(0).setCellValue("Component");
                            row0.createCell(1).setCellValue("line-rate");
                            row0.createCell(2).setCellValue("branch-rate");
                            row0.createCell(3).setCellValue("path");
                            i++;
                        }
                        Row row = sheet.createRow(i);
                        row.createCell(0).setCellValue(node.getName());
                        row.createCell(1).setCellValue(node.getValue(0));
                        row.createCell(2).setCellValue(node.getValue(1));
                        row.createCell(3).setCellValue(node.getPath());
                        i++;
                    }
                }

                if (tree.getType() == Tree.Type.BULLSEYE) {
                    sum += Double.parseDouble(node.getValue(0)) + Double.parseDouble(node.getValue(2)) + Double.parseDouble(node.getValue(4));
                    if (sum == 0.0) {
                        continue;
                    } else {
                        if (i == 0) {
                            Row row0 = sheet.createRow(0);
                            row0.createCell(0).setCellValue("Component");
                            row0.createCell(1).setCellValue("fn_cov");
                            row0.createCell(2).setCellValue("cd_cov");
                            row0.createCell(3).setCellValue("d_cov");
                            i++;
                        }
                        Row row = sheet.createRow(i);
                        row.createCell(0).setCellValue(node.getName());
                        row.createCell(1).setCellValue(node.getValue(0));
                        row.createCell(2).setCellValue(node.getValue(2));
                        row.createCell(3).setCellValue(node.getValue(4));
                        row.createCell(7).setCellValue(node.getPath());
                        i++;
                    }
                }
            }
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\filtered.xls");
            wb.write(fileOut);
            fileOut.close();
        } catch (
                FileNotFoundException e
                )

        {
            e.printStackTrace();
        } catch (
                InvalidFormatException e
                )

        {
            e.printStackTrace();
        } catch (
                IOException e
                )

        {
            e.printStackTrace();
        }
    }

    public static void getSubtree(Tree tree, int level){
        InputStream inp;
        try {
            inp = new FileInputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\filtered.xls");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            wb.setSheetName(0, "coberturaReport used methods");
            for (int index = sheet.getLastRowNum(); index >= sheet.getFirstRowNum(); index--) {
                if (sheet.getRow(index) == null) {
                    continue;
                }
                sheet.removeRow(sheet.getRow(index));
            }
            int i = 0;
            for (Node node : tree) {
                if (tree.getType() == Tree.Type.COBERTURA) {
                    if (node.getCurrentLevel() <= level) {
                        if (i == 0) {
                            Row row0 = sheet.createRow(0);
                            row0.createCell(0).setCellValue("Component");
                            row0.createCell(1).setCellValue("line-rate");
                            row0.createCell(2).setCellValue("branch-rate");
                            row0.createCell(3).setCellValue("path");
                            i++;
                        }
                        Row row = sheet.createRow(i);
                        row.createCell(0).setCellValue(node.getName());
                        row.createCell(1).setCellValue(node.getValue(0));
                        row.createCell(2).setCellValue(node.getValue(1));
                        row.createCell(3).setCellValue(node.getPath());
                        i++;
                    }
                }

                if (tree.getType() == Tree.Type.BULLSEYE) {
                    if (node.getCurrentLevel() <= level) {
                        if (i == 0) {
                            Row row0 = sheet.createRow(0);
                            row0.createCell(0).setCellValue("Component");
                            row0.createCell(1).setCellValue("fn_cov");
                            row0.createCell(2).setCellValue("cd_cov");
                            row0.createCell(3).setCellValue("d_cov");
                            i++;
                        }
                        Row row = sheet.createRow(i);
                        row.createCell(0).setCellValue(node.getName());
                        row.createCell(1).setCellValue(node.getValue(0));
                        row.createCell(2).setCellValue(node.getValue(2));
                        row.createCell(3).setCellValue(node.getValue(4));
                        row.createCell(7).setCellValue(node.getPath());
                        i++;
                    }
                }
            }
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\filtered.xls");
            wb.write(fileOut);
            fileOut.close();
        } catch (
                FileNotFoundException e
                )

        {
            e.printStackTrace();
        } catch (
                InvalidFormatException e
                )

        {
            e.printStackTrace();
        } catch (
                IOException e
                )

        {
            e.printStackTrace();
        }
    }

    public static void diff(Tree one, Tree two) {
        InputStream inp;
        try {
            inp = new FileInputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\workbook3.xls");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            wb.setSheetName(0, "compare 2 reports");
            for (int index = sheet.getLastRowNum(); index >= sheet.getFirstRowNum(); index--) {
                if (sheet.getRow(index) == null) {
                    continue;
                }
                sheet.removeRow(sheet.getRow(index));
            }
            int i = 0;
            for (Node node : one) {
                Node node2 = two.getNode(node.getPath());
                if (one.getType() == Tree.Type.COBERTURA) {
                    if (i == 0) {
                        Row row0 = sheet.createRow(0);
                        row0.createCell(0).setCellValue("path");
                        row0.createCell(1).setCellValue("line-rate difference");
                        row0.createCell(2).setCellValue("branch-rate difference");
                        i++;
                    }
                    Row row = sheet.createRow(i);

                    if (node2 == null) {
                        row.createCell(0).setCellValue(node.getPath());
                        row.createCell(1).setCellValue(Double.parseDouble(node.getValue(0)));
                        row.createCell(2).setCellValue(Double.parseDouble(node.getValue(1)));
                    } else {
                        Double tmp = Double.parseDouble(node.getValue(0)) - Double.parseDouble(node2.getValue(0));
                        Double tmp1 = Double.parseDouble(node.getValue(1)) - Double.parseDouble(node2.getValue(1));
                        if (tmp1 < 0 || tmp < 0) {
                            row.createCell(0).setCellValue(node.getPath());
                            row.createCell(1).setCellValue(tmp);
                            row.createCell(2).setCellValue(tmp1);i++;
                        }
                    }

                }

                if (one.getType() == Tree.Type.BULLSEYE) {
                    if (i == 0) {
                        Row row0 = sheet.createRow(0);
                        row0.createCell(0).setCellValue("path");
                        row0.createCell(1).setCellValue("fn_cov, first pack");
                        row0.createCell(2).setCellValue("fn_cov, second pack");
                        row0.createCell(3).setCellValue("fn_cov difference");
                        row0.createCell(4).setCellValue("cd_cov, first pack");
                        row0.createCell(5).setCellValue("cd_cov, second pack");
                        row0.createCell(6).setCellValue("cd_cov difference");
                        row0.createCell(7).setCellValue("d_cov, first pack");
                        row0.createCell(8).setCellValue("d_cov, second pack");
                        row0.createCell(9).setCellValue("d_cov difference");
                        i++;
                    }
                    Row row = sheet.createRow(i);
                    row.createCell(0).setCellValue(node.getPath());
                    row.createCell(1).setCellValue(node.getValue(0));
                    row.createCell(4).setCellValue(node.getValue(1));
                    row.createCell(7).setCellValue(node.getValue(2));
                    if (node2 == null) {
                        row.createCell(2).setCellValue("NaN");
                        row.createCell(3).setCellValue(node.getValue(0));
                        row.createCell(5).setCellValue("NaN");
                        row.createCell(6).setCellValue(node.getValue(1));
                        row.createCell(8).setCellValue("Nan");
                        row.createCell(9).setCellValue(node.getValue(2));
                    } else {
                        row.createCell(2).setCellValue(node2.getValue(0));
                        row.createCell(3).setCellValue(Double.parseDouble(node.getValue(0)) - Double.parseDouble(node2.getValue(0)));
                        row.createCell(5).setCellValue(node2.getValue(1));
                        row.createCell(6).setCellValue(Double.parseDouble(node.getValue(1)) - Double.parseDouble(node2.getValue(1)));
                        row.createCell(8).setCellValue(node2.getValue(2));
                        row.createCell(3).setCellValue(Double.parseDouble(node.getValue(2)) - Double.parseDouble(node2.getValue(2)));
                        i++;
                    }
                }
            }
            if (two.size() > one.size()) {
                for (Node node : two) {
                    if (!node.wasIterated()) {
                        if (two.getType() == Tree.Type.BULLSEYE) {
                            Row row = sheet.createRow(i);
                            row.createCell(0).setCellValue(node.getPath());
                            row.createCell(1).setCellValue(node.getValue(0));
                            row.createCell(4).setCellValue(node.getValue(1));
                            row.createCell(7).setCellValue(node.getValue(2));
                            row.createCell(2).setCellValue("NaN");
                            row.createCell(3).setCellValue(node.getValue(0));
                            row.createCell(5).setCellValue("NaN");
                            row.createCell(6).setCellValue(node.getValue(1));
                            row.createCell(8).setCellValue("Nan");
                            row.createCell(9).setCellValue(node.getValue(2));
                        } else if (two.getType() == Tree.Type.COBERTURA) {
                            Row row = sheet.createRow(i);
                            row.createCell(0).setCellValue(node.getPath());
                            row.createCell(1).setCellValue(node.getValue(0));
                            row.createCell(4).setCellValue(node.getValue(1));
                            row.createCell(2).setCellValue("NaN");
                            row.createCell(3).setCellValue(Double.parseDouble(node.getValue(0)));
                            row.createCell(5).setCellValue("NaN");
                            row.createCell(6).setCellValue(Double.parseDouble(node.getValue(1)));
                        }
                    }
                }
            }
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\ekaterina.lashutina\\Desktop\\workbook3.xls");
            wb.write(fileOut);
            fileOut.close();


        } catch (
                FileNotFoundException e
                )

        {
            e.printStackTrace();
        } catch (
                InvalidFormatException e
                )

        {
            e.printStackTrace();
        } catch (
                IOException e
                )

        {
            e.printStackTrace();
        }
    }
}
